package tt.dz.mvc.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;

import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.system.OmsOperList;
import tt.dz.mvc.controller.base.BaseAction;
import tt.dz.util.ActionUtils;
import tt.dz.util.OmsConstant.MsgCodeEnum;

/**
 * Servlet Filter implementation class LoginFilter
 */
//@WebFilter(
//		urlPatterns = { "/*" }, 
//		initParams = { 
//				@WebInitParam(name = "noFilterUrls", value = "userApi/login"),
//				@WebInitParam(name = "loginPath", value = "login.html"),
//		})
public class LoginFilter implements Filter 
{
	/**
	 * 不需要拦截的路径
	 */
	private String[] noFilterUrls;
	
	private String loginPath;
    /**
     * Default constructor. 
     */
    public LoginFilter() 
    {
    	
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException 
	{
		HttpServletRequest req = (HttpServletRequest)request;
		HttpServletResponse rep = (HttpServletResponse)response;
		//获取到当前登录的用户
		
		String uri = req.getRequestURI();
		if (noFilterUrls != null)
		{
			for (String nUrl : noFilterUrls)
			{
				if (uri.contains(nUrl))
				{
					chain.doFilter(request, response);
					return ;
				}
			}
		}
			HttpSession session = req.getSession(); 
			OmsOperList oper = (OmsOperList) session.getAttribute(BaseAction.CommonParam.SESSION_USER); 
			if (oper == null)
			{
				//如果判断Http头部 ajax信息 是ajax请求，返回403信息
//				if (req.getHeader("x-requested-with") != null 
//                        && "XMLHttpRequest".equalsIgnoreCase(req.getHeader("x-requested-with"))) {   
//                    //向http头添加 状态 sessionstatus
//					rep.setHeader("sessionstatus","timeout");
//					rep.setStatus(403);
//                    //向http头添加登录的url
//					rep.addHeader("loginPath", loginPath);
//                    chain.doFilter(request, response);
//                    return ;
//                }
//				//否则为其他页面，返回相应的login页面
//				rep.sendRedirect(loginPath);
				JsonCommon<?> jc = new JsonCommon<>();
//				jc.setPojo(MsgCodeEnum.SESSION_TIME_OUT.getMsgInfo());
				jc.setCode(MsgCodeEnum.SESSION_TIME_OUT.getMsgCode());
				jc.setMsg(MsgCodeEnum.SESSION_TIME_OUT.getMsgInfo());
				ActionUtils.doSystemAction(req, rep, loginPath, jc);
			} 
			else
			{
				chain.doFilter(request, response);
			}
	
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException 
	{
		String qUrls = fConfig.getInitParameter("noFilterUrls");
		if (StringUtils.isNotEmpty(qUrls))
		{
			noFilterUrls = StringUtils.split(qUrls, ",");
		}
		loginPath = fConfig.getInitParameter("loginPath");
	}

}
